const request = require("request");
const common = appRequire('controllers/wx/common');


const userModel = appRequire('models/user');

// 暴露WeChat对象
module.exports = {
    sendJzzsRecordTemplateMsg
};


//推送提醒下载模板
async function sendJzzsRecordTemplateMsg(req) {
    let postData = req.body;
    let access_token = await common.getAccessToken(req);
    let url = `https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=` + access_token; //发送模板消息的接口

    let thisUser = await userModel.user.findByPk(postData.uid)

    let requestData = {
        touser: thisUser.wcMiniId,
        template_id: 'jj4SU56sI2TQBMg63btH3HOuOrSV3blQznWxmaKOHDY',
        page: "/pages/tarBar/class",
        data: {
            thing1: {
                value: "资源更新",
                color: "#173177"
            },

            thing4: {
                value: "昨日新增资源" + postData.pptNum + "个",
                color: '#1d1d1d'
            },
            time6: {
                value: dateFormat(Date.parse(new Date()), 'yyyy-mm-dd HH:MM:ss'),
                color: '#1d1d1d'
            },
        }
    }

    if (thisUser.wcMiniId) {
        requestData.touser = thisUser.wcMiniId
        request({
            url: url,
            method: 'post',
            body: JSON.stringify(requestData),
        }, function (error, res, body) {
            console.log(error, body)
            body = JSON.parse(body)
            if (!error && body.errcode == 0) {
                console.log("模板消息发送成功")
                return true
            }
        });
    } else {
        return false
    }
}